package com.mvc.business.regedit;

import com.mvc.business.entity.GoodsForm;
import com.mvc.framework.action.IAction;
import com.mvc.framework.annotation.Controller;
import com.mvc.framework.entity.ActionFom;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class GetGoodsAction implements IAction {
    private static Map<String, Double> goodsMap;

    @Override
    public String excute(HttpServletRequest request, ActionFom actionFom, Map<String, String> forward) {
        Map goods = getGoods();
        GoodsForm goodsForm = (GoodsForm) actionFom;
        if (goodsForm.getGoodsName() == null || "".equalsIgnoreCase(goodsForm.getGoodsName()) || goods.get(goodsForm.getGoodsName()) == null) {
            return forward.get("failure");
        }
        goodsForm.setPrice(Double.parseDouble(goods.get(goodsForm.getGoodsName()) == null ? "0" : goods.get(goodsForm.getGoodsName()).toString()));
        request.setAttribute("goods", goodsForm);
        return forward.get("success");
    }

    private Map<String, Double> getGoods() {
        if (goodsMap == null) {
            goodsMap = new HashMap<>();
            goodsMap.put("香蕉", 8d);
            goodsMap.put("苹果", 10d);
            goodsMap.put("橘子", 3d);
            goodsMap.put("芒果", 20d);
        }
        return goodsMap;
    }
}
